﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using HISModels.Entities;
using KeanuMvc.Reference;
using HISModels.ViewModel;

namespace HISRestServices.Services {

    [ServiceContract()]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class Master : BaseServices, IDisposable {

        #region Field
        private HttpStatusCode statusCode = new HttpStatusCode();
        private string detailMessage = string.Empty;
        #endregion

        #region Banks
        [WebGet(UriTemplate = "bank/{id}")]
        public HttpResponseMessage<Bank> GetBankById(int id) {
            HttpResponseMessage<Bank> returnValue = null;
            Bank bank = null;
            try {
                bank = new Bank();

                var a = (from b in AppContext.Banks.Include("Addresses")
                         where b.Id == id
                         select b).First();

                bank = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<Bank>(statusCode);
            returnValue.Content = new ObjectContent<Bank>(bank);
            if (bank == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            return returnValue;

        }

        [WebInvoke(Method = "POST", UriTemplate = "bank/paging/")]
        public HttpResponseMessage<DataTablesNet> GetBankPaging(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string code = string.Empty;

            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                code = data[0].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.Banks
                        where b.RecordStatus == 1
                        select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(code));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "bank/")]
        public HttpResponseMessage<Bank> InsertBank(Bank paramObject) {
            HttpResponseMessage<Bank> returnValue = null;
            string HeaderMessageKey = string.Empty;
            try {
                HeaderMessageKey = HISModels.Enums.ResponseMessage.DetailHeadesMessageKey;
                if (AppContext.Banks.Where(d => d.NameValue == paramObject.NameValue && d.RecordStatus == 1).Count() > 0) {

                    detailMessage = "Data is already exist in Database!";

                } else {
                    paramObject.RecordStatus = 1;
                    paramObject.CategoryId = AppContext.RefPayerCategories.Where(p => p.NameValue.Contains("Private")).First().Id;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    foreach (var item in paramObject.Addresses) {
                        paramObject.RecordStatus = 1;
                        AppContext.Entry(item).State = System.Data.EntityState.Added;
                    }
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;

                }
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Bank>(statusCode);
                returnValue.Content = new ObjectContent<Bank>(paramObject);
                returnValue.Headers.Add(HeaderMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "bank/")]
        public HttpResponseMessage<Bank> UpdateBank(Bank paramObject) {
            HttpResponseMessage<Bank> returnValue = null;
            try {
                if (AppContext.Suppliers.Where(d => d.CodeValue == paramObject.NameValue && d.Id != paramObject.Id && d.RecordStatus != 0).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    //foreach (var item in paramObject.Addresses)
                    //{
                    //    paramObject.RecordStatus = 1;
                    //    AppContext.Entry(item).State = System.Data.EntityState.Modified;
                    //}

                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    statusCode = HttpStatusCode.OK;
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Bank>(statusCode);
                returnValue.Content = new ObjectContent<Bank>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "bank/")]
        public HttpResponseMessage<Bank> DeleteBankByID(int id) {
            HttpResponseMessage<Bank> returnValue = null;
            Bank bank = null;
            try {
                bank = AppContext.Banks.Where(d => d.Id == id).First();

                bank.RecordStatus = 0;
                AppContext.Entry(bank).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Bank>(statusCode);
                returnValue.Content = new ObjectContent<Bank>(bank);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "bank/list/")]
        public HttpResponseMessage<List<Bank>> GetListBank() {
            HttpResponseMessage<List<Bank>> returnValue = null;
            List<Bank> listBank = null;
            try {
                listBank = AppContext.Banks.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<Bank>>(statusCode);
            returnValue.Content = new ObjectContent<List<Bank>>(listBank);
            if (listBank == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }

        #endregion

        #region Items

        //item
        [WebGet(UriTemplate = "item/{id}")]
        public HttpResponseMessage<Item> GetItemByCode(int id) {
            HttpResponseMessage<Item> returnValue = null;
            Item item = null;
            try {
                item = AppContext.Items.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Item>(statusCode);
                returnValue.Content = new ObjectContent<Item>(item);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string code = string.Empty;
            string nama = string.Empty;


            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                code = data[0].Split('=')[1].NullSafeToString();
                nama = data[1].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.Items where b.RecordStatus == 1 select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(nama) && p.CodeValue.Contains(code));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.CodeValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/")]
        public HttpResponseMessage<Item> InsertItem(Item paramObject) {
            HttpResponseMessage<Item> returnValue = null;
            try {
                if (AppContext.Items.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Item>(statusCode);
                returnValue.Content = new ObjectContent<Item>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/")]
        public HttpResponseMessage<Item> UpdateItem(Item paramObject) {
            HttpResponseMessage<Item> returnValue = null;
            try {
                if (AppContext.Items.Where(d => d.CodeValue == paramObject.CodeValue && d.Id != paramObject.Id).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Item>(statusCode);
                returnValue.Content = new ObjectContent<Item>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/")]
        public HttpResponseMessage<Item> DeleteItemByID(int id) {
            HttpResponseMessage<Item> returnValue = null;
            Item item = null;
            try {
                item = AppContext.Items.Where(d => d.Id == id).First();

                item.RecordStatus = 0;
                AppContext.Entry(item).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Item>(statusCode);
                returnValue.Content = new ObjectContent<Item>(item);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        //item sub category
        [WebGet(UriTemplate = "item/subcategory/{id}")]
        public HttpResponseMessage<RefItemSubCategory> GetItemSubCategoryById(int id) {
            HttpResponseMessage<RefItemSubCategory> returnValue = null;
            RefItemSubCategory itemgroup = null;
            try {
                itemgroup = AppContext.RefItemSubCategories.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemSubCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemSubCategory>(itemgroup);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/subcategory/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemSubCategoryAll(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string code = string.Empty;
            string nama = string.Empty;
            string kat = string.Empty;
            int category = -1;


            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                nama = data[0].Split('=')[1].NullSafeToString();
                code = data[1].Split('=')[1].NullSafeToString();

                kat = data[2].Split('=')[1].NullSafeToString();
                if (kat !="")
                {
                    category = Convert.ToInt32(kat);
                }
               
                var a = from b in AppContext.RefItemSubCategories
                        join c in AppContext.RefItemCategories on b.ItemCategoryId equals c.Id
                        where b.RecordStatus == 1
                        select new
                        {
                            b.Id,
                            b.NameValue,
                            b.CodeValue,
                            b.ItemCategoryId,
                            category = c.Id,
                            CategoryName=c.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(nama) && p.CodeValue.Contains(code));

                if (category !=-1)
                {
                    a = a.Where(p => p.category == category);
                }

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.CodeValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/subcategory/")]
        public HttpResponseMessage<RefItemSubCategory> InsertItemSubCategory(RefItemSubCategory paramObject)
        {
            HttpResponseMessage<RefItemSubCategory> returnValue = null;
            try {
                if (AppContext.RefItemSubCategories.Where(d => d.NameValue == paramObject.NameValue || d.CodeValue == paramObject.CodeValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemSubCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemSubCategory>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/subcategory/")]
        public HttpResponseMessage<RefItemSubCategory> UpdateItemSubCategory(RefItemSubCategory paramObject)
        {
            HttpResponseMessage<RefItemSubCategory> returnValue = null;
            try {
                if (AppContext.RefItemSubCategories.Where(d => d.CodeValue == paramObject.CodeValue && d.Id != paramObject.Id).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemSubCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemSubCategory>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/subcategory/")]
        public HttpResponseMessage<RefItemSubCategory> DeleteItemSubCategoryByID(int id)
        {
            HttpResponseMessage<RefItemSubCategory> returnValue = null;
            RefItemSubCategory itemgroup = null;
            try {
                itemgroup = AppContext.RefItemSubCategories.Where(d => d.Id == id).First();

                itemgroup.RecordStatus = 0;
                AppContext.Entry(itemgroup).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemSubCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemSubCategory>(itemgroup);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "item/subcategory/list/")]
        public HttpResponseMessage<List<RefItemSubCategory>> GetListItemSubCategory() {
            HttpResponseMessage<List<RefItemSubCategory>> returnValue = null;
            List<RefItemSubCategory> listSubCategory = null;
            try {
                listSubCategory = AppContext.RefItemSubCategories.Where(p => p.RecordStatus != 0).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefItemSubCategory>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefItemSubCategory>>(listSubCategory);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        //item shapes
        [WebGet(UriTemplate = "item/shape/{id}")]
        public HttpResponseMessage<RefItemShape> GetItemShapeById(int id) {
            HttpResponseMessage<RefItemShape> returnValue = null;
            RefItemShape itemshape = null;
            try {
                itemshape = AppContext.RefItemShapes.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemShape>(statusCode);
                returnValue.Content = new ObjectContent<RefItemShape>(itemshape);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/shape/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemShapeAll(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string code = string.Empty;
            string nama = string.Empty;
            string desc = string.Empty;


            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                code = data[0].Split('=')[1].NullSafeToString();
                nama = data[1].Split('=')[1].NullSafeToString();
                desc = data[2].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.RefItemShapes where b.RecordStatus == 1 select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(nama) && p.CodeValue.Contains(code) && p.DescValue != null ? p.DescValue.Contains(desc) : true);

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.CodeValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/shape/")]
        public HttpResponseMessage<RefItemShape> InsertItemShape(RefItemShape paramObject) {
            HttpResponseMessage<RefItemShape> returnValue = null;
            try {
                if (AppContext.RefItemShapes.Where(d => d.NameValue == paramObject.NameValue || d.CodeValue == paramObject.CodeValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemShape>(statusCode);
                returnValue.Content = new ObjectContent<RefItemShape>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/shape/")]
        public HttpResponseMessage<RefItemShape> UpdateItemShape(RefItemShape paramObject) {
            HttpResponseMessage<RefItemShape> returnValue = null;
            try {
                if (AppContext.RefItemShapes.Where(d => d.CodeValue == paramObject.CodeValue && d.Id != paramObject.Id).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemShape>(statusCode);
                returnValue.Content = new ObjectContent<RefItemShape>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/shape/")]
        public HttpResponseMessage<RefItemShape> DeleteItemShapeByID(int id) {
            HttpResponseMessage<RefItemShape> returnValue = null;
            RefItemShape itemshape = null;
            try {
                itemshape = AppContext.RefItemShapes.Where(d => d.Id == id).First();

                itemshape.RecordStatus = 0;
                AppContext.Entry(itemshape).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemShape>(statusCode);
                returnValue.Content = new ObjectContent<RefItemShape>(itemshape);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "item/shape/list/")]
        public HttpResponseMessage<List<RefItemShape>> GetListItemShape() {
            HttpResponseMessage<List<RefItemShape>> returnValue = null;
            List<RefItemShape> listItemShape = null;
            try {
                listItemShape = AppContext.RefItemShapes.Where(p => p.RecordStatus != 0).ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefItemShape>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefItemShape>>(listItemShape);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        //item supplier

        [WebInvoke(Method = "POST", UriTemplate = "itemsupplier")]
        public HttpResponseMessage<ItemSupplierRel> InsertItemSupplier(ItemSupplierRel paramObject) {
            HttpResponseMessage<ItemSupplierRel> returnValue = null;
            try {
                if (AppContext.ItemSupplierRels.Where(d => d.ItemId == paramObject.ItemId && d.SupplierId == paramObject.SupplierId).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<ItemSupplierRel>(statusCode);
                returnValue.Content = new ObjectContent<ItemSupplierRel>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "itemsupplier/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemSupplierAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            int idItems = 0;
            string code = string.Empty;

            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                code = data[0].Split('=')[1].NullSafeToString();

                if (code != "") {
                    idItems = Convert.ToInt32(code);
                }

                var a = from b in AppContext.Suppliers
                        join c in AppContext.ItemSupplierRels on b.Id equals c.SupplierId
                        join d in AppContext.Items on c.ItemId equals d.Id 
                        where c.RecordStatus == 1
                        select new {
                            c.Id,
                            b.CodeValue,
                            b.NameValue,
                            c.ItemId
                        };

                if (idItems != -1) {
                    a = a.Where(e => (e.ItemId == idItems));
                }

          a = a.OrderBy(e => e.ItemId);
            
                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "itemsupplier")]
        public HttpResponseMessage<ItemSupplierRel> DeleteItemSupplierByID(int id) {
            HttpResponseMessage<ItemSupplierRel> returnValue = null;
            ItemSupplierRel itemSupplier = null;
            try {
                itemSupplier = AppContext.ItemSupplierRels.Where(d => d.Id == id).First();

                itemSupplier.RecordStatus = 0;
                AppContext.Entry(itemSupplier).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<ItemSupplierRel>(statusCode);
                returnValue.Content = new ObjectContent<ItemSupplierRel>(itemSupplier);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        

        //item types
        [WebGet(UriTemplate = "item/type/{id}")]
        public HttpResponseMessage<RefItemType> GetItemTypeById(int id) {
            HttpResponseMessage<RefItemType> returnValue = null;
            RefItemType itemtype = null;
            try {
                itemtype = AppContext.RefItemTypes.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemType>(statusCode);
                returnValue.Content = new ObjectContent<RefItemType>(itemtype);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/type/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemTypeAll(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string desc = string.Empty;
            string codevalue = string.Empty;


            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                desc = data[0].Split('=')[1].NullSafeToString();
                codevalue = data[1].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.RefItemTypes where b.RecordStatus == 1 select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => (p.DescValue != null ? p.DescValue.Contains(desc) : false) && (p.CodeValue.Contains(codevalue)));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.CodeValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/type/")]
        public HttpResponseMessage<RefItemType> InsertItemType(RefItemType paramObject) {
            HttpResponseMessage<RefItemType> returnValue = null;
            try {
                if (AppContext.RefItemTypes.Where(d => d.DescValue == paramObject.DescValue || d.CodeValue == paramObject.CodeValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemType>(statusCode);
                returnValue.Content = new ObjectContent<RefItemType>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/type/")]
        public HttpResponseMessage<RefItemType> UpdateItemType(RefItemType paramObject) {
            HttpResponseMessage<RefItemType> returnValue = null;
            try {
                if (AppContext.RefItemShapes.Where(d => d.CodeValue == paramObject.CodeValue && d.DescValue == paramObject.DescValue && d.Id != paramObject.Id).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemType>(statusCode);
                returnValue.Content = new ObjectContent<RefItemType>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/type/")]
        public HttpResponseMessage<RefItemType> DeleteItemTypeByID(int id) {
            HttpResponseMessage<RefItemType> returnValue = null;
            RefItemType itemtype = null;
            try {
                itemtype = AppContext.RefItemTypes.Where(d => d.Id == id).First();

                itemtype.RecordStatus = 0;
                AppContext.Entry(itemtype).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<RefItemType>(statusCode);
                returnValue.Content = new ObjectContent<RefItemType>(itemtype);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }



        //item category
        [WebGet(UriTemplate = "item/category/{id}")]
        public HttpResponseMessage<RefItemCategory> GetItemCategoryById(int id)
        {
            HttpResponseMessage<RefItemCategory> returnValue = null;
            RefItemCategory itemtype = null;
            try
            {
                itemtype = AppContext.RefItemCategories.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefItemCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemCategory>(itemtype);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/category/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemCategoryAll(PostDataTablesNet pagingSearch)
        {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string namevalue = string.Empty;
            string codevalue = string.Empty;


            try
            {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                namevalue = data[0].Split('=')[1].NullSafeToString();
                codevalue = data[1].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.RefItemCategories where b.RecordStatus == 1 select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => (p.CodeValue != null ? p.CodeValue.Contains(codevalue) : true) && (p.NameValue.Contains(namevalue)));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.CodeValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/category/")]
        public HttpResponseMessage<RefItemCategory> InsertItemCategory(RefItemCategory paramObject)
        {
            HttpResponseMessage<RefItemCategory> returnValue = null;
            try
            {
                if (AppContext.RefItemCategories.Where(d => d.NameValue == paramObject.NameValue || d.CodeValue == paramObject.CodeValue).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefItemCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemCategory>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/category/")]
        public HttpResponseMessage<RefItemCategory> UpdateItemCategory(RefItemCategory paramObject)
        {
            HttpResponseMessage<RefItemCategory> returnValue = null;
            try
            {
                if (AppContext.RefItemCategories.Where(d => d.CodeValue == paramObject.CodeValue && d.NameValue == paramObject.NameValue && d.Id != paramObject.Id).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefItemCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemCategory>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/category/")]
        public HttpResponseMessage<RefItemCategory> DeleteItemCategoryByID(int id)
        {
            HttpResponseMessage<RefItemCategory> returnValue = null;
            RefItemCategory itemcategories = null;
            try
            {
                itemcategories = AppContext.RefItemCategories.Where(d => d.Id == id).First();

                itemcategories.RecordStatus = 0;
                AppContext.Entry(itemcategories).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefItemCategory>(statusCode);
                returnValue.Content = new ObjectContent<RefItemCategory>(itemcategories);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "item/category/list/")]
        public HttpResponseMessage<List<RefItemCategory>> GetListCategory()
        {
            HttpResponseMessage<List<RefItemCategory>> returnValue = null;
            List<RefItemCategory> listCategory = null;
            try
            {
                listCategory = AppContext.RefItemCategories.Where(p => p.RecordStatus != 0).ToList();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<RefItemCategory>>(statusCode);
            returnValue.Content = new ObjectContent<List<RefItemCategory>>(listCategory);
            if (listCategory == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        } 


        //item location
        [WebGet(UriTemplate = "item/location/{id}")]
        public HttpResponseMessage<ItemLocation> GetItemLocationByCode(int id)
        {
            HttpResponseMessage<ItemLocation> returnValue = null;
            ItemLocation itemlocation = null;
            try
            {
                itemlocation = AppContext.ItemLocations.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemLocation>(statusCode);
                returnValue.Content = new ObjectContent<ItemLocation>(itemlocation);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/location/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemLocationAllPost(PostDataTablesNet pagingSearch)
        {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;

            string nama = string.Empty;
            string site = string.Empty;
            int siteid = -1;



            try
            {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                nama = data[0].Split('=')[1].NullSafeToString();

                site = data[1].Split('=')[1].NullSafeToString();
                if (site != "")
                {
                    siteid = Convert.ToInt32(site);
                }


                var a = from b in AppContext.ItemLocations where b.RecordStatus == 1 select b;

                if (siteid != -1)
                {
                    a = a.Where(p => p.ItemSiteId == siteid);
                }

                a = a.Where(p => p.NameValue.Contains(nama));

                paging.iTotalRecords = a.Count();              

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.NameValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/location/")]
        public HttpResponseMessage<ItemLocation> InsertItemLocation(ItemLocation paramObject)
        {
            HttpResponseMessage<ItemLocation> returnValue = null;
            try
            {
                if (AppContext.ItemLocations.Where(d => d.NameValue == paramObject.NameValue).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemLocation>(statusCode);
                returnValue.Content = new ObjectContent<ItemLocation>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/location/")]
        public HttpResponseMessage<ItemLocation> UpdateItemLocation(ItemLocation paramObject)
        {
            HttpResponseMessage<ItemLocation> returnValue = null;
            try
            {
                if (AppContext.ItemLocations.Where(d => d.NameValue == paramObject.NameValue && d.Id != paramObject.Id).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemLocation>(statusCode);
                returnValue.Content = new ObjectContent<ItemLocation>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/location/")]
        public HttpResponseMessage<ItemLocation> DeleteItemLocationByID(int id)
        {
            HttpResponseMessage<ItemLocation> returnValue = null;
            ItemLocation itemlocation = null;
            try
            {
                itemlocation = AppContext.ItemLocations.Where(d => d.Id == id).First();

                itemlocation.RecordStatus = 0;
                AppContext.Entry(itemlocation).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemLocation>(statusCode);
                returnValue.Content = new ObjectContent<ItemLocation>(itemlocation);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }


        //item site
        [WebGet(UriTemplate = "item/site/{id}")]
        public HttpResponseMessage<ItemSite> GetItemSiteById(int id)
        {
            HttpResponseMessage<ItemSite> returnValue = null;
            ItemSite itemsite = null;
            try
            {
                itemsite = AppContext.ItemSites.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemSite>(statusCode);
                returnValue.Content = new ObjectContent<ItemSite>(itemsite);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/site/paging/")]
        public HttpResponseMessage<DataTablesNet> GetItemSiteAllPost(PostDataTablesNet pagingSearch)
        {

            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] param = null;

            int huid = 0;
            string hospitalid = string.Empty;
            string itemsite = string.Empty;


            try
            {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                param = pagingSearch.sSearch.Split(';');


                itemsite = param[0].Split('=')[1].NullSafeToString();
                hospitalid = param[1].Split('=')[1].NullSafeToString();

                if (hospitalid != "")
                {
                    huid = Convert.ToInt32(hospitalid);
                }

                var a = from b in AppContext.ItemSites
                        join c in AppContext.RefHus on b.HospitalUnitId equals c.Id
                        where b.RecordStatus != 0
                        select new
                        {
                            b.Id,
                            b.NameValue,
                            b.HospitalUnitId,
                            Location = c.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(itemsite));

                if (huid > -1)
                {
                    a = a.Where(p => p.HospitalUnitId == huid);
                }

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.NameValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);

            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "item/site/")]
        public HttpResponseMessage<ItemSite> InsertItemSite(ItemSite paramObject)
        {
            HttpResponseMessage<ItemSite> returnValue = null;
            try
            {
                if (AppContext.ItemSites.Where(d => d.NameValue == paramObject.NameValue && d.RecordStatus != 0).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemSite>(statusCode);
                returnValue.Content = new ObjectContent<ItemSite>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "item/site/")]
        public HttpResponseMessage<ItemSite> UpdateItemSite(ItemSite paramObject)
        {
            HttpResponseMessage<ItemSite> returnValue = null;
            try
            {
                if (AppContext.ItemSites.Where(d => d.NameValue == paramObject.NameValue && d.HospitalUnitId == d.HospitalUnitId && d.Id != paramObject.Id).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemSite>(statusCode);
                returnValue.Content = new ObjectContent<ItemSite>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "item/site/")]
        public HttpResponseMessage<ItemSite> DeleteItemSiteByID(int id)
        {
            HttpResponseMessage<ItemSite> returnValue = null;
            ItemSite itemsite = null;
            try
            {
                itemsite = AppContext.ItemSites.Where(d => d.Id == id).First();

                itemsite.RecordStatus = 0;
                AppContext.Entry(itemsite).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<ItemSite>(statusCode);
                returnValue.Content = new ObjectContent<ItemSite>(itemsite);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }


        #endregion

        #region Departement
        [WebGet(UriTemplate = "departement/{id}")]
        public HttpResponseMessage<Departement> GetDepartementByID(int id) {
            HttpResponseMessage<Departement> returnValue = null;
            Departement departement = null;
            try {
                departement = AppContext.Departements.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Departement>(statusCode);
                returnValue.Content = new ObjectContent<Departement>(departement);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "departement/paging/")]
        public HttpResponseMessage<DataTablesNet> GetDepartementAllPost(PostDataTablesNet pagingSearch)
        {

            DataTablesNet paging = null;
            HttpResponseMessage<DataTablesNet> returnValue = null;
            string[] param = null;

            int huid = 0;
            string hospitalid = string.Empty;
            string departement = string.Empty;


            try
            {
                paging = new DataTablesNet();
                paging.sEcho = pagingSearch.sEcho;
                param = pagingSearch.sSearch.Split(';');


                departement = param[0].Split('=')[1].NullSafeToString();
                hospitalid = param[1].Split('=')[1].NullSafeToString();

                if (hospitalid != "")
                {
                    huid = Convert.ToInt32(hospitalid);
                }   


                //var a = from b in AppContext.SysRoles where b.RoleId > 0 select b;
                var a = from b in AppContext.Departements
                        join c in AppContext.RefHus on b.HospitalUnitId equals c.Id
                        select new
                        {
                            b.Id,
                            b.NameValue,
                            b.HospitalUnitId,
                            Location = c.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(departement));

                if (huid > -1)
                {
                    a = a.Where(p => p.HospitalUnitId == huid);
                }

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.NameValue);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
            returnValue.Content = new ObjectContent<DataTablesNet>(paging);

            if (paging == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "departement/")]
        public HttpResponseMessage<Departement> InsertDepartement(Departement paramObject) {
            HttpResponseMessage<Departement> returnValue = null;
            try {
                if (AppContext.Departements.Where(d => d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Departement>(statusCode);
                returnValue.Content = new ObjectContent<Departement>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "departement/")]
        public HttpResponseMessage<Departement> UpdateDepartement(Departement paramObject) {
            HttpResponseMessage<Departement> returnValue = null;
            try {
                if (AppContext.Departements.Where(d => d.NameValue == paramObject.NameValue && d.HospitalUnitId == paramObject.HospitalUnitId && d.Id != paramObject.Id).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Departement>(statusCode);
                returnValue.Content = new ObjectContent<Departement>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "departement/")]
        public HttpResponseMessage<Departement> DeleteDepartementByID(int id) {
            HttpResponseMessage<Departement> returnValue = null;
            Departement departement = null;
            try {
                departement = AppContext.Departements.Where(d => d.Id == id).First();

                //departement.RecordStatus = 0;
                AppContext.Entry(departement).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Departement>(statusCode);
                returnValue.Content = new ObjectContent<Departement>(departement);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "departement/list/")]
        public HttpResponseMessage<List<Departement>> GetListDepartement()
        {
            HttpResponseMessage<List<Departement>> returnValue = null;
            List<Departement> listDepartement = null;
            try
            {
                listDepartement = AppContext.Departements.ToList();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<Departement>>(statusCode);
            returnValue.Content = new ObjectContent<List<Departement>>(listDepartement);
            if (listDepartement == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        } 

        #endregion

        #region Employee

        [WebGet(UriTemplate = "employee/{huid}")]
        public HttpResponseMessage<Employee> GetEmployeeByHuId(int huid) {
            HttpResponseMessage<Employee> returnValue = null;
            Employee employee = null;
            try {
                employee = new Employee();

                var a = (from b in AppContext.Employees
                         where b.HospitalUnitId == huid
                         select b).First();

                employee = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Employee>(statusCode);
                returnValue.Content = new ObjectContent<Employee>(employee);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;

        }

        [WebInvoke(Method = "POST", UriTemplate = "employee/paging/")]
        public HttpResponseMessage<DataTablesNet> GetEmployeeAllPost(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;

            string employeenumber = string.Empty;
            string nama = string.Empty;
            int huid = -1;
            string hospitalid = string.Empty;

            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                employeenumber = data[0].Split('=')[1].NullSafeToString();
                nama = data[1].Split('=')[1].NullSafeToString();

                hospitalid = data[2].Split('=')[1].NullSafeToString();
                if (hospitalid !="")
                {
                    huid = Convert.ToInt32(hospitalid);
                }


                var a = from b in AppContext.Employees
                        join c in AppContext.RefGenders on b.GenderId  equals c.Id
                        join d in AppContext.Departements on b.DepartementId equals d.Id
                        join e in AppContext.RefHus on b.HospitalUnitId equals e.Id
                        where b.RecordStatus==1
                        select new
                        {
                            b.Id,
                            b.HospitalUnitId,
                            b.EmployeeNumber,
                            b.GenderId, 
                            b.FirstName,
                            b.LastName,
                            b.SuperiorNumber,
                            fullname = b.FirstName+" "+b.LastName,
                            gender = c.NameValue,
                            departementid = d.Id,
                            departement = d.NameValue,
                            hospital = e.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => (p.fullname.Contains(nama) && p.EmployeeNumber.Contains(employeenumber)));

                if (huid != -1) {
                    a = a.Where(p => p.HospitalUnitId == huid);
                }

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.HospitalUnitId);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "employee/")]
        public HttpResponseMessage<Employee> InsertEmployee(Employee paramObject) {
            HttpResponseMessage<Employee> returnValue = null;
            string HeaderMessageKey = string.Empty;
            try {
                HeaderMessageKey = HISModels.Enums.ResponseMessage.DetailHeadesMessageKey;
                if (AppContext.Employees.Where(d => d.Id ==paramObject.Id).Count() > 0) {

                    detailMessage = "Data is already exist in Database!";

                } else {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;

                }
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Employee>(statusCode);
                returnValue.Content = new ObjectContent<Employee>(paramObject);
                returnValue.Headers.Add(HeaderMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "employee/")]
        public HttpResponseMessage<Employee> UpdateEmployee(Employee paramObject) {
            HttpResponseMessage<Employee> returnValue = null;
            try {
                if (AppContext.Employees.Where(d => d.EmployeeNumber == paramObject.EmployeeNumber && d.Id != paramObject.Id && d.RecordStatus != 0).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    foreach (var doctor in paramObject.Doctors) {
                        foreach (var docspecrel in doctor.DoctorSpecRels) {
                            AppContext.Entry(docspecrel).State = System.Data.EntityState.Modified;
                        }
                        AppContext.Entry(doctor).State = System.Data.EntityState.Modified;
                    }
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    statusCode = HttpStatusCode.OK;
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<Employee>(statusCode);
                returnValue.Content = new ObjectContent<Employee>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "employee/")]
        public HttpResponseMessage<Employee> DeleteEmployeeByID(int id)
        {
            HttpResponseMessage<Employee> returnValue = null;
            Employee employee = null;
            try
            {
                employee = AppContext.Employees.Where(d => d.Id == id).First();

                employee.RecordStatus = 0;
                AppContext.Entry(employee).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<Employee>(statusCode);
                returnValue.Content = new ObjectContent<Employee>(employee);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "employee/detail/{id}")]
        public HttpResponseMessage<Employee> GetEmployeeById(int id)
        {
            HttpResponseMessage<Employee> returnValue = null;
            Employee employee = null;
            try
            {
                employee = new Employee();

                var a = (from b in AppContext.Employees
                         where b.Id == id
                         select b).First();

                employee = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<Employee>(statusCode);
                returnValue.Content = new ObjectContent<Employee>(employee);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;

        }

        #endregion

        #region Medication Action
         //Medication Action
        [WebGet(UriTemplate = "medicalvocab/action/{id}")]
        public HttpResponseMessage<RefMedicalVocab> GetMedicationActionById(int id)
        {
            HttpResponseMessage<RefMedicalVocab> returnValue = null;
            RefMedicalVocab medicAction = null;
            try
            {
                medicAction = AppContext.RefMedicalVocabs.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefMedicalVocab>(statusCode);
                returnValue.Content = new ObjectContent<RefMedicalVocab>(medicAction);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "medicalvocab/action/paging/")]
        public HttpResponseMessage<DataTablesNet> GetMedicationActionAll(PostDataTablesNet pagingSearch)
        {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string namevalue = string.Empty;

            try
            {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');

                namevalue = data[0].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.RefMedicalVocabs select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.NameValue.Contains(namevalue));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Id);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "medicalvocab/action/")]
        public HttpResponseMessage<RefMedicalVocab> InsertMedicationAction(RefMedicalVocab paramObject)
        {
            HttpResponseMessage<RefMedicalVocab> returnValue = null;
            try
            {
                if (AppContext.RefMedicalVocabs.Where(d => d.NameValue == paramObject.NameValue).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefMedicalVocab>(statusCode);
                returnValue.Content = new ObjectContent<RefMedicalVocab>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "medicalvocab/action/")]
        public HttpResponseMessage<RefMedicalVocab> UpdateMedicationAction(RefMedicalVocab paramObject)
        {
            HttpResponseMessage<RefMedicalVocab> returnValue = null;
            try
            {
                if (AppContext.RefItemCategories.Where(d => d.NameValue == paramObject.NameValue && d.Id != paramObject.Id).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefMedicalVocab>(statusCode);
                returnValue.Content = new ObjectContent<RefMedicalVocab>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "medicalvocab/action/")]
        public HttpResponseMessage<RefMedicalVocab> DeleteMedicationActionByID(int id)
        {
            HttpResponseMessage<RefMedicalVocab> returnValue = null;
            RefMedicalVocab medicaction = null;
            try
            {
                medicaction = AppContext.RefMedicalVocabs.Where(d => d.Id == id).First();

                AppContext.Entry(medicaction).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<RefMedicalVocab>(statusCode);
                returnValue.Content = new ObjectContent<RefMedicalVocab>(medicaction);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "medicalvocab/action/list/")]
        public HttpResponseMessage<List<RefMedicalVocab>> GetListMedicationAction()
        {
            HttpResponseMessage<List<RefMedicalVocab>> returnValue = null;
            List<RefMedicalVocab> listMedicAction = null;
            try {
                listMedicAction = AppContext.RefMedicalVocabs.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<RefMedicalVocab>>(statusCode);
                returnValue.Content = new ObjectContent<List<RefMedicalVocab>>(listMedicAction);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
    
            }
            return returnValue;
        }


        //Synonim Tag
        [WebGet(UriTemplate = "synonimtag/{id}")]
        public HttpResponseMessage<SynonimTag> GetSynonimTagById(int id) {
            HttpResponseMessage<SynonimTag> returnValue = null;
            SynonimTag synonimtag = null;
            try {
                synonimtag = AppContext.SynonimTags.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<SynonimTag>(statusCode);
                returnValue.Content = new ObjectContent<SynonimTag>(synonimtag);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "synonimtag/paging/")]
        public HttpResponseMessage<DataTablesNet> GetSynonimTagAll(PostDataTablesNet pagingSearch) {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string code = string.Empty;
            string nama = string.Empty;
            string MAId = string.Empty;
            int id = -1;


            try {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                nama = data[0].Split('=')[1].NullSafeToString();
                MAId = data[1].Split('=')[1].NullSafeToString();

                if (MAId != string.Empty) {
                    id = Convert.ToInt32(MAId);
                }

                var a = from b in AppContext.SynonimTags
                        join c in AppContext.RefMedicalVocabs on b.MedicalVocabId equals c.Id
                        select new {
                            b.Id,
                            Synonim = b.NameValue,
                            MedActionId = b.MedicalVocabId,
                            MedActionName = c.NameValue
                        };

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.Synonim.Contains(nama));

                if (id != -1) {
                    a = a.Where(p => p.MedActionId == id);
                }

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.Synonim);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "synonimtag/")]
        public HttpResponseMessage<SynonimTag> InsertSynonimTag(SynonimTag paramObject) {
            HttpResponseMessage<SynonimTag> returnValue = null;
            try {
                if (AppContext.SynonimTags.Where(d => d.NameValue == paramObject.NameValue && d.MedicalVocabId == paramObject.MedicalVocabId).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<SynonimTag>(statusCode);
                returnValue.Content = new ObjectContent<SynonimTag>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "synonimtag/")]
        public HttpResponseMessage<SynonimTag> UpdateSynonimTag(SynonimTag paramObject) {
            HttpResponseMessage<SynonimTag> returnValue = null;
            try {
                if (AppContext.SynonimTags.Where(d => d.Id != paramObject.Id && d.NameValue == paramObject.NameValue).Count() > 0) {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                } else {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<SynonimTag>(statusCode);
                returnValue.Content = new ObjectContent<SynonimTag>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "synonimtag/")]
        public HttpResponseMessage<SynonimTag> DeleteSynonimTagByID(int id) {
            HttpResponseMessage<SynonimTag> returnValue = null;
            SynonimTag synonimtag = null;
            try {
                synonimtag = AppContext.SynonimTags.Where(d => d.Id == id).First();

                AppContext.Entry(synonimtag).State = System.Data.EntityState.Deleted;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<SynonimTag>(statusCode);
                returnValue.Content = new ObjectContent<SynonimTag>(synonimtag);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "synonimtag/list/")]
        public HttpResponseMessage<List<SynonimTag>> GetListSynonimTag() {
            HttpResponseMessage<List<SynonimTag>> returnValue = null;
            List<SynonimTag> listSubCategory = null;
            try {
                listSubCategory = AppContext.SynonimTags.ToList();
                statusCode = HttpStatusCode.OK;
            } catch (Exception ex) {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            } finally {
                returnValue = new HttpResponseMessage<List<SynonimTag>>(statusCode);
                returnValue.Content = new ObjectContent<List<SynonimTag>>(listSubCategory);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }
        #endregion

        #region PriceList
        [WebGet(UriTemplate = "pricelist/{id}")]
        public HttpResponseMessage<PriceList> GetPriceListById(int id)
        {
            HttpResponseMessage<PriceList> returnValue = null;
            PriceList pricelist = null;
            try
            {
                pricelist = AppContext.PriceLists.Where(d => d.Id == id).First();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<PriceList>(statusCode);
                returnValue.Content = new ObjectContent<PriceList>(pricelist);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "pricelist/paging/")]
        public HttpResponseMessage<DataTablesNet> GetPriceListAllPost(PostDataTablesNet pagingSearch)
        {
            HttpResponseMessage<DataTablesNet> returnValue = null;

            DataTablesNet paging = new DataTablesNet();
            string[] data = null;
            string chargename = string.Empty;


            try
            {
                paging.sEcho = pagingSearch.sEcho;
                data = pagingSearch.sSearch.Split(';');
                chargename = data[0].Split('=')[1].NullSafeToString();

                var a = from b in AppContext.PriceLists where b.RecordStatus == 1 select b;

                paging.iTotalRecords = a.Count();

                a = a.Where(p => p.ChargeName.Contains(chargename));

                paging.iTotalDisplayRecords = a.Count();

                a = a.OrderBy(b => b.ChargeName);
                a = a.Skip(pagingSearch.iDisplayStart).Take(pagingSearch.iDisplayLength);

                paging.aaData = a;
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<DataTablesNet>(statusCode);
                returnValue.Content = new ObjectContent<DataTablesNet>(paging);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "POST", UriTemplate = "pricelist/")]
        public HttpResponseMessage<PriceList> InsertPriceList(PriceList paramObject)
        {
            HttpResponseMessage<PriceList> returnValue = null;
            try
            {
                if (AppContext.PriceLists.Where(d => d.ChargeName == paramObject.ChargeName).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                    paramObject = null;
                }
                else
                {
                    paramObject.RecordStatus = 1;
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Added;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.InsertSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<PriceList>(statusCode);
                returnValue.Content = new ObjectContent<PriceList>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "PUT", UriTemplate = "pricelist/")]
        public HttpResponseMessage<PriceList> UpdatePriceList(PriceList paramObject)
        {
            HttpResponseMessage<PriceList> returnValue = null;
            try
            {
                if (AppContext.PriceLists.Where(d => d.ChargeName == paramObject.ChargeName && d.Id != paramObject.Id).Count() > 0)
                {
                    detailMessage = "Data is already exist in Database!";
                }
                else
                {
                    AppContext.Entry(paramObject).State = System.Data.EntityState.Modified;
                    AppContext.SaveChanges(UserId);
                    detailMessage = HISModels.Enums.ResponseMessage.UpdateSuccess;
                }
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<PriceList>(statusCode);
                returnValue.Content = new ObjectContent<PriceList>(paramObject);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }

            return returnValue;
        }

        [WebInvoke(Method = "DELETE", UriTemplate = "pricelist/")]
        public HttpResponseMessage<PriceList> DeletePriceListByID(int id)
        {
            HttpResponseMessage<PriceList> returnValue = null;
            PriceList pricelist = null;
            try
            {
                pricelist = AppContext.PriceLists.Where(d => d.Id == id).First();

                pricelist.RecordStatus = 0;
                AppContext.Entry(pricelist).State = System.Data.EntityState.Modified;
                AppContext.SaveChanges(UserId);

                detailMessage = HISModels.Enums.ResponseMessage.DeleteSuccess;
                statusCode = HttpStatusCode.OK;

            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            finally
            {
                returnValue = new HttpResponseMessage<PriceList>(statusCode);
                returnValue.Content = new ObjectContent<PriceList>(pricelist);
                returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            }
            return returnValue;
        }

        [WebGet(UriTemplate = "pricelist/list/")]
        public HttpResponseMessage<List<PriceList>> GetListPriceList()
        {
            HttpResponseMessage<List<PriceList>> returnValue = null;
            List<PriceList> listPrice = null;
            try
            {
                listPrice = AppContext.PriceLists.ToList();
                statusCode = HttpStatusCode.OK;
            }
            catch (Exception ex)
            {
                statusCode = HttpStatusCode.InternalServerError;
                detailMessage = ex.Message;
            }
            returnValue = new HttpResponseMessage<List<PriceList>>(statusCode);
            returnValue.Content = new ObjectContent<List<PriceList>>(listPrice);
            if (listPrice == null) returnValue.Headers.Add(HISModels.Enums.ResponseMessage.DetailHeadesMessageKey, detailMessage);
            return returnValue;
        }

        #endregion

        void IDisposable.Dispose() {
            try {
                base.Dispose();
                GC.SuppressFinalize(this);
            } catch { }
        }
    }
}